<template>
	<el-container>
		<el-aside width="220px">
			<el-tree ref="category"
					 class="menu"
					 node-key="label"
					 :data="category"
					 :default-expanded-keys="['项目状态']"
					 current-node-key="项目状态"
					 @node-click="updateProjectRecord"
					 :highlight-current="true"
					 :expand-on-click-node="false">
			</el-tree>
		</el-aside>
		<el-container>
			<el-main class="nopadding">
				<el-container>
					<el-header style="height: auto;">
						<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
					</el-header>
					<el-header>
						<div class="left-panel">
							<el-button type="primary" icon="el-icon-plus" @click="createProject">创建</el-button>
							<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
						</div>
						<div class="right-panel">
							<div class="right-panel-search">
								<el-input v-model="search.keyword" placeholder="编号 / 名称"></el-input>
								<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
							</div>
						</div>
					</el-header>
					<el-main class="nopadding">
						<scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter>
							<el-table-column type="selection" width="50"></el-table-column>
							<el-table-column label="#" type="index" width="50"></el-table-column>
							<el-table-column label="项目编号" prop="proCode" width="150" :show-overflow-tooltip="true"></el-table-column>
							<el-table-column label="项目名称" prop="title" width="200" :show-overflow-tooltip="true"></el-table-column>
							<el-table-column label="项目分类" prop="sortName" width="100"></el-table-column>
							<el-table-column label="项目等级" prop="proLevelName" width="100"></el-table-column>
							<el-table-column label="开始时间" prop="beginTime" width="150"></el-table-column>
							<el-table-column label="结束时间" prop="endTime" width="150"></el-table-column>
							<el-table-column label="项目经理" prop="managerUserName" width="100"></el-table-column>
							<el-table-column label="当前状态" prop="status" width="100">
								<template #default="scope">
									<el-tag type="danger" size="small" v-if="scope.row.status=='0'">立项中</el-tag>
									<el-tag type="warning" size="small" v-else-if="scope.row.status=='1'">已驳回</el-tag>
									<el-tag type="success" size="small" v-else-if="scope.row.status=='2'">审批中</el-tag>
									<el-tag type="primary" size="small" v-else-if="scope.row.status=='3'">处理中</el-tag>
									<el-tag type="warning" size="small" v-else-if="scope.row.status=='4'">已挂起</el-tag>
									<el-tag type="success" size="small" v-else-if="scope.row.status=='5'">已完成</el-tag>
									<el-tag type="info" size="small" v-else>未知</el-tag>
								</template>
							</el-table-column>
							<el-table-column label="操作" fixed="right" align="right" width="300">
								<template #default="scope">
									<el-button-group>
										<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
									</el-button-group>
									<el-button-group v-if="scope.row.status=='0' || scope.row.status=='1'">
										<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
										<el-button text type="primary" size="small" @click="decompose(scope.row, scope.$index)">分解</el-button>
										<el-button text type="primary" size="small" @click="approvalProRecord(scope.row, scope.$index)">提交</el-button>
										<el-button text type="primary" size="small" @click="table_del(scope.row, scope.$index)">删除</el-button>
									</el-button-group>
									<el-button-group v-if="scope.row.status=='2'">
										<el-button text type="primary" size="small" @click="takeBack(scope.row, scope.$index)">收回</el-button>
									</el-button-group>
								</template>
							</el-table-column>
						</scTable>
					</el-main>
				</el-container>
			</el-main>
		</el-container>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>
	<recordDetails v-if="dialog.detailsSave" ref="detailsDialog" @closed="dialog.detailsSave=false"></recordDetails>
</template>

<script>
import scSelectFilter from '@/components/scSelectFilter'
import saveDialog from './save'
import recordDetails from './details'
export default {
	name: 'Project Manage',
	components: {
		scSelectFilter,
		saveDialog,
		recordDetails
	},
	data() {
		return {
			dialog: {
				save: false,
				detailsSave:false
			},
			selection:[],
			filterData: [
				{
					title: "日期范围",
					key: "date",
					multiple: false,
					options: [
						{
							label: "全部",
							value: ""
						},
						{
							label: "今日",
							value: "1"
						},
						{
							label: "昨日",
							value: "2"
						},
						{
							label: "一周内",
							value: "3"
						},
						{
							label: "一月内",
							value: "4"
						},
						{
							label: "一月前",
							value: "5"
						}
					]
				},
				{
					title: "项目级别",
					key: "proLevel",
					multiple: false,
					options:[{
						label: "全部",
						value: ""
					}]
				}],
			category: [
				{
					label: '项目状态',
					children: [
						{value:'0',label: '立项中'},
						{value:'1',label: '已驳回'},
						{value:'2',label: '审批中'},
						{value:'3',label: '处理中'},
						{value:'4',label: '已挂起'},
						{value:'5',label: '已办结'},
					]
				}
			],
			date: [],
			apiObj: this.$API.project.proRecord.getProRecordListByStatus,
			search: {
				date:[],
				status:'',
				keyword: "",
				proLevel:""
			}
		}
	},
	mounted() {
		this.getProDicByCode();
	},
	methods: {
		//查看
		table_show(row) {
			this.dialog.detailsSave = true
			this.$nextTick(() => {
				this.$refs.detailsDialog.open('setting').setData(row)
			})
		},
		async takeBack(row,index){
			let res = await this.$API.project.proRecord.updateProRecord.post({proId:row.proId,status:"0"});
			if (res.code == 200) {
				this.$refs.table.refresh();
				this.$message.success("操作成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async approvalProRecord(row,index){
			let res = await this.$API.project.proRecord.approvalProRecord.post({proId:row.proId,status:"2"});
			if (res.code == 200) {
				this.$refs.table.refresh();
				this.$message.success("操作成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//删除
		async table_del(row) {
			var reqData = {proId: row.proId}
			var res = await this.$API.project.proRecord.deleteProRecord.post(reqData);
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//批量删除
		async batch_del() {
			await this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				let proIdArr = [];
				for (var i = 0; i < this.selection.length; i++) {
					if(this.selection[i].status=='0'||this.selection[i].status=='1')
					{
						proIdArr.push(this.selection[i].proId);
					}
				}
				if(proIdArr.length==0)
				{
					this.$alert("请选择可以删除的项！", "提示", {type: 'error'})
				}else
				{
					const loading = this.$loading();
					var res = await this.$API.project.proRecord.deleteProRecordByIds.post({ids:proIdArr});
					if (res.code == 200) {
						this.$refs.table.refresh()
						this.$message.success(res.message)
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
					loading.close();
				}
			}).catch(() => {

			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		decompose(row){
			this.$nextTick(() => {
				this.$router.replace({
					path: '/project/initiation/breakdown',
					query:{
						proId:row.proId
					}
				})
			})
		},
		filterChange(data){
			this.search.date = data.date;
			this.search.proLevel = data.proLevel;
			this.$refs.table.upData(this.search)
		},
		createProject(){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		async getProDicByCode()
		{
			var res = await this.$API.project.proDic.getProDicByCode.get({code:'proLevel'});
			if (res.code == 200) {
				this.filterData[1].options=this.filterData[1].options.concat(res.data);
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		upsearch(){
			this.$refs.table.upData(this.search)
		},
		updateProjectRecord(item)
		{
			this.search.status = item.value;
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSaveSuccess() {
			this.$refs.table.refresh()
		}
	}
}
</script>

<style>
</style>
